package org.opens.converter.mybatisJdbcType;

import org.opens.converter.AbstractConverterFactory;
import org.opens.converter.constant.ConverterConfig;
import org.opens.converter.mybatisJdbcType.handle.MysqlMybatisJdbcTypeConverterHandle;
import org.opens.converter.mybatisJdbcType.handle.OracleMybatisJdbcTypeConverterHandle;
import org.opens.converter.sqlType.I_SqlTypeConverter;
import org.opens.db.constant.DbType;
import org.opens.exception.BusinessException;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述
 *
 * @author by zhangyuming
 * @date 2023/3/7
 */
public class MybatisJdbcTypeConverterFactory extends AbstractConverterFactory {

    private static Map<DbType, I_MybatisJdbcTypeConverter> mybatisJdbcTypeConverterMap = new HashMap<>(ConverterConfig.defaultConverterMapSize);

    static {
        mybatisJdbcTypeConverterMap.put(DbType.MYSQL, new MysqlMybatisJdbcTypeConverterHandle());
        mybatisJdbcTypeConverterMap.put(DbType.ORACLE, new OracleMybatisJdbcTypeConverterHandle());
    }

    public static I_MybatisJdbcTypeConverter getInstance(DbType dbType) {
        I_MybatisJdbcTypeConverter mybatisJdbcTypeConverter = mybatisJdbcTypeConverterMap.get(dbType);
        if (mybatisJdbcTypeConverter == null) {
            throw new BusinessException("根据数据库类型" + dbType.getTypeName() + "未获取到有效的类型映射数据, 请先增加转换规则!");
        }
        return mybatisJdbcTypeConverter;
    }

    @Override
    public I_SqlTypeConverter getSqlTypeConverter(DbType dbType) {
        throw new RuntimeException("no support");
    }

    @Override
    public I_MybatisJdbcTypeConverter getMybatisJdbcTypeConverter(DbType dbType) {
        return MybatisJdbcTypeConverterFactory.getInstance(dbType);
    }
}
